<template>
  <el-dialog
    :title="formData.id ? '编辑用户' : '新增用户'"
    :visible="dialogVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    center
  >
    <el-form
      ref="addEditForm"
      v-loading="isLoading > 0"
      :model="formData"
      :rules="rules"
      label-width="100px"
      class="add-edit-form"
    >
      <el-form-item label="微信openid" prop="openid">
        <span>{{ formData.openid }}</span>
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickname">
        <el-input v-model="formData.nickname" />
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <img :src="formData.avatar" style="height: 100px;" alt />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="formData.mobile" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="formData.address" />
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-date-picker v-model="formData.birthday" type="date" placeholder="选择日期" />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="formData.gender" placeholder="选择性别">
          <el-option
            v-for="item in gender_list"
            :key="item.id"
            :label="item.gender_name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="余额" prop="amount">
        <el-input v-model="formData.amount" />
      </el-form-item>
      <el-form-item label="积分" prop="bonus_points">
        <el-input v-model="formData.bonus_points" />
      </el-form-item>
      <el-form-item label="注册时间">
        <span>{{ formData.create_time }}</span>
      </el-form-item>
      <el-form-item label="是否上线">
        <el-switch
          :value="formData.status === 1"
          active-color="#13ce66"
          inactive-color="#ff4949"
          @change="formData.status = formData.status === 1 ? -1 : 1"
        />
      </el-form-item>

      <el-form-item class="submit-box">
        <el-button type="primary" @click="submitFormPre()">提交</el-button>
        <el-button v-if="!formData.id" @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
const formData = {
  openid: 'test0000000001',
  nickname: '',
  avatar: '',
  mobile: '',
  address: '',
  birthday: '',
  gender: '',
  amount: '',
  bonus_points: '',
  create_time: new Date(),
  status: -1
}
import { addOneUser, updateOneUser } from '@/api/user'
import CONST from '@/utils/const'
export default {
  name: 'UserAddEdit',
  components: {},
  props: {},
  data() {
    return {
      gender_list: CONST.GENDER_LIST,
      dialogVisible: false,
      isLoading: 0,
      formData: {},
      rules: {
        nickname: [
          { required: true, message: '不能为空', trigger: ['change', 'blur'] }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
  },
  mounted() {},
  methods: {
    handleClose() {
      this.dialogVisible = false
    },
    open(row) {
      this.dialogVisible = true
      this.formData = row ? { ...row } : { ...formData }
      this.$nextTick(() => {
        this.$refs.addEditForm.clearValidate()
      })
    },
    resetForm() {
      this.formData = { ...formData }
      this.$refs.addEditForm.resetFields()
    },
    submitFormPre() {
      this.$prompt('确定要进行此次操作么？请输入『确定』', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async({ value }) => {
        if (value === '确定') {
          this.submitForm()
        } else {
          this.$message.error('输入错误')
        }
      })
    },
    submitForm() {
      if (this.isLoading > 0) return
      this.$refs.addEditForm.validate(async vaild => {
        if (!vaild) return
        try {
          this.isLoading++
          if (this.formData.id) {
            await updateOneUser(this.formData)
          } else {
            await addOneUser(this.formData)
          }
          this.$emit('refresh')
          this.handleClose()
        } finally {
          this.isLoading--
        }
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.submit-box {
  display: flex;
  justify-content: flex-end;
}
</style>
